package com.example.guiguiapplicatio.fragment.followup.adapter;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.FragmentFollowUpRecyItemBinding;
import com.example.guiguiapplicatio.databindingBean.followup.ListFollowupWith;
import com.example.guiguiapplicatio.utils.BirthdayUtils;

import java.io.Serializable;
import java.util.List;


public class FlowUpFragmentAdapter extends RecyclerView.Adapter<FlowUpFragmentAdapter.ViewHolder> {
    private List<ListFollowupWith> mValues;
    private NavController navController;
    public FlowUpFragmentAdapter(List<ListFollowupWith> mValues) {
        this.mValues = mValues;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        navController= Navigation.findNavController(parent);
        return new ViewHolder(FragmentFollowUpRecyItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final int mPosition=position;
        holder.mItem=mValues.get(position);
        holder.binding.age.setText(BirthdayUtils.extractYearMonthDayOfIdCard(holder.mItem.getFollowUp().getIdCard()));
        holder.binding.sex.setText(holder.mItem.getSex());
        holder.binding.name.setText(holder.mItem.getPatientName());
        holder.binding.phone.setText(holder.mItem.getPatientPhone());
        if (holder.mItem.getFollowUp().getIdCard().length()>16){

            String show_id = holder.mItem.getFollowUp().getIdCard().substring(0, 3) + "********" + holder.mItem.getFollowUp().getIdCard().substring(11);
            holder.binding.idcard.setText(show_id);
        }else  holder.binding.idcard.setText(holder.mItem.getFollowUp().getIdCard());
        holder.binding.date.setText(holder.mItem.getFollowUp().getEditTime());
        holder.binding.adress.setText(holder.mItem.getPatientResidence());
        holder.binding.type.setText(holder.mItem.getFollowUp().getFollowUpType());

        if (holder.mItem.getFollowUp().getState()==1){
            holder.binding.state.setText("正式随访");
            holder.binding.headStye.setBackgroundResource(R.drawable.border_head);
            holder.binding.background.setBackgroundResource(R.drawable.border_background);

        }else if (holder.mItem.getFollowUp().getState()==0){
            holder.binding.state.setText("随访草稿");
            holder.binding.headStye.setBackgroundResource(R.drawable.border_head_gray);
            holder.binding.background.setBackgroundResource(R.drawable.border_background_gray);
        }else {
            holder.binding.state.setText("已作废");
        }
        holder.binding.recyItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle =new Bundle();
                bundle.putSerializable("ListFollowupWith",mValues.get(mPosition));
                bundle.putBoolean("IS_EXIST_FOLLOWUP_TO_THIS",true);
                Log.e("onClick", "onClick: "+mValues.get(mPosition).getFollowUp().getIdCard());


                if (mValues.get(mPosition).getFollowUp().getFollowUpType().equals("新生儿")){
                    navController.navigate(R.id.action_navigation_follow_up_to_newChildFragment,bundle);
                }
                if (mValues.get(mPosition).getFollowUp().getFollowUpType().equals("高血压")){
                    navController.navigate(R.id.action_navigation_follow_up_to_hypertensionFragment,bundle);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        FragmentFollowUpRecyItemBinding binding;
        public ListFollowupWith mItem;
        public ViewHolder(@NonNull FragmentFollowUpRecyItemBinding binding) {
            super(binding.getRoot());
            this.binding=binding;
        }
    }
}
